$(document).ready(function(){
    $('#start').click(function(){
        $('#text').append('Working ...!!!');
        $('#response').css('display','block');
        $.ajax({
            cache: false,
            type: 'post',
            url: 'progress/sendmails.php',
            beforeSend: start_display()
        });
    });
	$('#start').click(); //Ejecutamos automaticamente ...sin hacer clic
});

var interval='';

function start_display() {
    if (interval==""){
        interval=window.setInterval("display()",200);
	$('#loader').css('display','inline');
    } else {
        stop_display();
    }
}

function stop_display() {
    if (interval!="") {
        window.clearInterval(interval);
        interval="";
    }
}

function display() {
    $.ajax({
        cache: false,
        type: 'get',
        url: 'progress/display.php',
        success: function(response) {
            $('#response div').css('width',response+'%');
            if (response=='100') {
                stop_display();
                $('#text').html('Finalizada la tarea !!!');
            }
        }
    });
}
